package com.sskj.gocoin.p.fragment;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.sskj.gocoin.MyAppLication;
import com.sskj.gocoin.R;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.m.HttpConfig;
import com.sskj.gocoin.m.bean.HttpData;
import com.sskj.gocoin.m.bean.LevelBean;
import com.sskj.gocoin.m.bean.TradePositionListBean;
import com.sskj.gocoin.m.http.JsonCallBack;
import com.sskj.gocoin.p.base.BasePresenter;
import com.sskj.gocoin.utils.ToastUtil;
import com.sskj.gocoin.v.fragment.NewTradePositionFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lee on 2018/2/28 0028.
 */
public class NewTradePositionFragmentPst extends BasePresenter<NewTradePositionFragment> {
    public void loadData(String code) {
//        Log.i("info123", "code:::::::" + code);
        // 获取持仓数据
        OkGo.<HttpData<List<TradePositionListBean>>>post(HttpConfig.BASE_URL + HttpConfig.TRADE_POSITION_LIST)
                .params("mobile", MyAppLication.getMobile())
                .params("code", code)
                .execute(new JsonCallBack<HttpData<List<TradePositionListBean>>>() {
                    @Override
                    public void _onSuccess(Response<HttpData<List<TradePositionListBean>>> response) {

                        HttpData<List<TradePositionListBean>> httpListData = response.body();
                        if (httpListData != null) {
                            mView.updateData(httpListData.getData());
                        }
                    }

                    @Override
                    protected void _onNewsSuccess(Response<HttpData<List<TradePositionListBean>>> response) {
                        super._onNewsSuccess(response);
                        if (App.INSTANCE.getString(R.string.strNewTradePositionFragmentPst204).equals(response.body().getMsg())) {
                            mView.updateData(new ArrayList<>());
                        }
                    }
                });
    }

    public void pingcang(TradePositionListBean data) {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.TRADE_PINGCANG)
                .params("mobile", MyAppLication.getMobile())
                .params("order_id", data.getHold_id())
                .params("newprice", data.getNewprice())
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData> response) {
                        HttpData httpListData = response.body();
                        ToastUtil.showShort(httpListData.getMsg());
                        if (httpListData.getStatus() == 200) {
                            mView.onPingChangSuccess(data);
                            loadData(mView.code);
                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });
    }


    public void setPoint(String orderId, String stopwin, String stoploss) {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.SET_POINT)
                .params("h_id", orderId)
                .params("zy", stopwin)
                .params("zs", stoploss)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    protected void _onSuccess(Response<HttpData> response) {
                        mView.onSetPointSuccess(response.body().getMsg());
                    }
                });
    }

    public void getLeveRange(String code, int otype, double priceDifference, String holdId, String poit_win, String poit_loss) {
        OkGo.<HttpData<LevelBean>>post(HttpConfig.BASE_URL + HttpConfig.LEVEL)
                .params("code", code)
                .execute(new JsonCallBack<HttpData<LevelBean>>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData<LevelBean>> response) {
                        mView.onLeveSuccess(response.body().getData(), otype, priceDifference, holdId, poit_win, poit_loss);
                    }
                });
    }
}
